package com.kaibes.platform.article.db.service;

import java.math.BigInteger;
import java.util.List;

import org.springframework.stereotype.Service;

import com.kaibes.orm.core.core.Page;
import com.kaibes.orm.curd.OrderType;
import com.kaibes.orm.curd.QueryBuilder;
import com.kaibes.orm.curd.SelectBuilder;
import com.kaibes.platform.article.db.entity.ArticleComment;
import com.kaibes.web.api.ResponseServiceImpl;

@Service
public class ArticleCommentService extends ResponseServiceImpl<ArticleComment> {
    
    public Page<ArticleComment> pageDescBySearch(int pageIndex, int pageSize, String search) {
        return getMapper().page(SelectBuilder.newBuilder()
                .addQuery(QueryBuilder.newBuilder().like("username", search).build())
                .addQuery(QueryBuilder.newBuilder().like("content", search).build())
                .build()
                , pageIndex, pageSize);
    }
    
    public Page<ArticleComment> pageDescAllByArticleId(int pageIndex, int pageSize, BigInteger articleId) {
        return getMapper().page(SelectBuilder.newBuilder().addQuery(QueryBuilder.newBuilder().eq("article_id", articleId).build()).addOrder("create_time", OrderType.DESC).build(), pageIndex, pageSize);
    }
    
    public Integer getUserIdById(BigInteger id) {
        return getById(id, "user_id").getUserId();
    }
    
    public ArticleComment getUserIdArticleIdById(BigInteger id) {
        return getById(id, "user_id", "article_id");
    }
    
    public long countByUserIdAndArticleId(Integer userId, BigInteger articleId) {
        return count(QueryBuilder.newBuilder().eq("user_id", userId).eq("article_id", articleId).build());
    }
    
    public List<ArticleComment> listByArticleIdList(List<BigInteger> articleIds) {
        return list(QueryBuilder.newBuilder().inList("article_id", articleIds).build());
    }
    
    public int removeByArticleIdList(List<?> articleIds) {
        return remove(QueryBuilder.newBuilder().inList("article_id", articleIds).build());
    }
    
}
